<?php

namespace App\Admin\Controllers\Dedusting;

use App\Models\DedustingAlarmLog;
use App\Models\DedustingAlarm;
use App\Models\DedustingDevicePoint;
use App\Models\DedustingDevice;
use App\Models\DedustingLibrary;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use App\Handlers\IotCardHandler;

class AlarmLogController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('报警记录列表')
            ->description('')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(DedustingAlarmLog::with(['device', 'admin']), function (Grid $grid) {
            $grid->column('device.name', '报警设备');
            $grid->column('point', '报警点位')->using(DedustingDevicePoint::POINTS);
            $grid->column('alarm_name','报警名称');
            $grid->column('alarm_level', '报警等级')->using(DedustingAlarm::LEVEL_TEXT);
            $grid->column('alarm_value','报警值');
            $grid->column('real_value','实际值');
            $grid->column('status','状态')->using(DedustingAlarmLog::STATUS_TEXT);
            $grid->column('admin.username','处理人');
            $grid->column('created_at', '报警时间');
            $grid->column('updated_at', '处理时间');

            $grid->disableCreateButton();
        });
    }

    public function update($id){
        return $this->form()->update($id);
    }
    
    public function form(){
        return Form::make(new DedustingAlarmLog(), function (Form $form) {
            $form->radio('status', '状态')->options(DedustingAlarmLog::STATUS_TEXT)->required();
            $form->hidden('operator')->value(Admin::user()->id);

            $form->saving(function (Form $form) {
                $form->operator = Admin::user()->id;
            });

        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('报警处理')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    public function show($id, Content $content)
    {
        return $content->header('报警记录')
            ->description('详情')
            ->body(Show::make($id, DedustingAlarmLog::with(['device']), function (Show $show) {
                $show->field('device.name', '报警设备');
                $show->field('point', '报警点位')->using(DedustingDevicePoint::POINTS);
                $show->field('alarm_name','报警名称');
                $show->field('alarm_level', '报警等级')->using(DedustingAlarm::LEVEL_TEXT);
                $show->field('alarm_value','报警值');
                $show->field('real_value','实际值');
                $show->field('status','状态')->using(DedustingAlarmLog::STATUS_TEXT);
                $show->field('created_at', '报警时间');
                $show->field('updated_at', '处理时间');

                $show->panel()->tools(function ($tools) {
                    $tools->disableEdit();
                });

            }));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }
}